package com.hmall.item.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.dto.SearchFilterDTO;
import com.hmall.common.vo.FilterParamVo;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 分页查询商品信息
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDTO<Item> listPage(Long page, Long size) {
        //设置分页查询条件
        Page<Item> itemPage = new Page<>(page, size);
        Page<Item> selectPage = itemMapper.selectPage(itemPage, null);
        return new PageDTO<>(selectPage.getTotal(), selectPage.getRecords());
    }

    /**
     * 更新商品状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatusById(Long id, Integer status) {
        Item item = itemMapper.selectById(id);
        item.setStatus(status);
        itemMapper.updateById(item);
        rabbitTemplate.convertAndSend("itemStatus.queue",status);
    }

    /**
     * 过滤项聚合
     *
     * @param filterParamVo
     * @return
     */
    @Override
    public SearchFilterDTO searchFilter(FilterParamVo filterParamVo) {
        SearchFilterDTO searchFilterDTO = new SearchFilterDTO();
        HashSet<String> brandList = new HashSet<>();
        HashSet<String> categoryList = new HashSet<>();
        if (filterParamVo.getCategory() != null) {
            // 根据分类查询品牌
            brandList = itemMapper.searchBrand(filterParamVo);
            searchFilterDTO.setBrand(brandList);
        } else if (filterParamVo.getBrand() != null) {
            // 根据品牌查询分类
            categoryList = itemMapper.searchCategory(filterParamVo);
            searchFilterDTO.setCategory(categoryList);
        } else {
            List<Item> items = itemMapper.searchFilter(filterParamVo);
            for (Item item : items) {
                brandList.add(item.getBrand());
                categoryList.add(item.getCategory());
            }
            searchFilterDTO.setBrand(brandList);
            searchFilterDTO.setCategory(categoryList);
        }

        return searchFilterDTO;
    }

    /**
     * 条件查询
     *
     * @param filterParamVo
     * @return
     */
    @Override
    public PageDTO<Item> searchListItem(FilterParamVo filterParamVo) {
        if (filterParamVo.getCategory() != null || filterParamVo.getBrand() != null || !filterParamVo.getKey().equals("")) {
            LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(filterParamVo.getKey() != null, Item::getName, filterParamVo.getKey())
                    .like(filterParamVo.getCategory() != null, Item::getCategory, filterParamVo.getCategory())
                    .like(filterParamVo.getBrand() != null, Item::getBrand, filterParamVo.getBrand())
                    .between(filterParamVo.getMinPrice() != null && filterParamVo.getMaxPrice() != null, Item::getPrice, filterParamVo.getMinPrice(), filterParamVo.getMaxPrice());
            Page<Item> itemPage = itemMapper.selectPage(new Page<>(filterParamVo.getPage(), filterParamVo.getSize()), wrapper);
            return new PageDTO<>(itemPage.getTotal(), itemPage.getRecords());
        } else {
            //返回redis中的数据
            String str = redisTemplate.opsForValue().get("top20");
            List<Item> ts = (List<Item>) JSONArray.parseArray(str, Item.class);
            return new PageDTO<>(20L, ts);
        }
    }
}
